﻿using SixLabors.Fonts;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ls.ImageSharep.App
{
    public partial class UCCamera : UserControl
    {
        public UCCamera()
        {
            InitializeComponent();
        }

        private void btntext_Click(object sender, EventArgs e)
        {
            using (SixLabors.ImageSharp.Image image =new  SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgba32>(700,400))
            {

                PathBuilder pathBuilder = new PathBuilder();
                pathBuilder.SetOrigin(new SixLabors.ImageSharp.PointF(200, 100));
                //pathBuilder.AddBezier(new PointF(50, 450), new PointF(200, 50), new PointF(300, 50), new PointF(450, 450));
                // add more complex paths and shapes here.
                pathBuilder.AddLine(new SixLabors.ImageSharp.PointF(100, 50), new SixLabors.ImageSharp.PointF(100, 350));


                IPath path = pathBuilder.Build();

              

                // For production application we would recomend you create a FontCollection
                // singleton and manually install the ttf fonts yourself as using SystemFonts
                // can be expensive and you risk font existing or not existing on a deployment
                // by deployment basis.
                var font = LsApp.GetFont(20);
                //SystemFonts.CreateFont("Arial", 39, FontStyle.Regular);

                string text = "你好，土豆先生《2022》";
               

                // lets generate the text as a set of vectors drawn along the path


                var glyphs = TextBuilder.GenerateGlyphs(text, path, new TextOptions(font)
                {
                    HorizontalAlignment = SixLabors.Fonts.HorizontalAlignment.Center,
                  
                    VerticalAlignment = SixLabors.Fonts.VerticalAlignment.Center,
                    WrappingLength = path.ComputeLength(),
                   // ApplyKerning = textGraphicsOptions.TextOptions.ApplyKerning
                });

                image.Mutate(ctx => ctx
                    .Fill(SixLabors.ImageSharp.Color.White) // white background image
                                       //.Draw(Color.Gray, 3, path) // draw the path so we can see what the text is supposed to be following
                                       // .Fill(Color.Black, glyphs)
                  .Fill(SixLabors.ImageSharp.Color.Black, glyphs)
                   );

                picbox.Image = System.Drawing.Image.FromStream(LsApp.ImgToMemoryStream(image));
                //picbox.SizeMode = PictureBoxSizeMode.AutoSize;
            }
        }

        #region 私有方法


        #endregion

        private void btntxt2_Click(object sender, EventArgs e)
        {
            using (SixLabors.ImageSharp.Image image = new SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgba32>(700, 500))
            {

                PathBuilder pathBuilder = new PathBuilder();
                //pathBuilder.SetOrigin(new SixLabors.ImageSharp.PointF(100, 50));
                pathBuilder.AddCubicBezier(new SixLabors.ImageSharp.PointF(100, 400), new SixLabors.ImageSharp.PointF(200, 0), 
                    new SixLabors.ImageSharp.PointF(250, 0), new SixLabors.ImageSharp.PointF(400, 400));
                // add more complex paths and shapes here.
                


                IPath path = pathBuilder.Build();



                // For production application we would recomend you create a FontCollection
                // singleton and manually install the ttf fonts yourself as using SystemFonts
                // can be expensive and you risk font existing or not existing on a deployment
                // by deployment basis.
                var font = LsApp.GetFont(20);
                //SystemFonts.CreateFont("Arial", 39, FontStyle.Regular);

                string text = "你好，土豆先生《2022》 你好，土豆先生《2022》  你好，土豆先生《2022》 你好，土豆先生《2022》";


                // lets generate the text as a set of vectors drawn along the path


                var glyphs = TextBuilder.GenerateGlyphs(text, path, new TextOptions(font)
                {
                    HorizontalAlignment = SixLabors.Fonts.HorizontalAlignment.Left,

                    VerticalAlignment = SixLabors.Fonts.VerticalAlignment.Top,
                    WrappingLength = path.ComputeLength(),
                   
                });

                image.Mutate(ctx => ctx
                    .Fill(SixLabors.ImageSharp.Color.White) // white background image
                                                            .Draw(SixLabors.ImageSharp.Color.Gray, 3, path) // draw the path so we can see what the text is supposed to be following
                  .Fill(SixLabors.ImageSharp.Color.Black, glyphs)
                   );

                picbox.Image = System.Drawing.Image.FromStream(LsApp.ImgToMemoryStream(image));
                //picbox.SizeMode = PictureBoxSizeMode.AutoSize;
            }
        }

        private void bntwjx_Click(object sender, EventArgs e)
        {
            using (SixLabors.ImageSharp.Image image = new SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgba32>(700, 500))
            {

                var outerRadii = Math.Min(image.Width, image.Height) / 2;
                var star = new Star(new SixLabors.ImageSharp.PointF(image.Width / 2, image.Height / 2), 5, outerRadii / 2, outerRadii);

                // we want to clone out our source image so we can apply 
                // various effects to it without mutating the original yet.
                using (var clone = image.Clone(p =>
                {
                    p.GaussianBlur(15); // apply the effect here you and inside the shape
                }))
                {
                    // crop the cloned down to just the size of the shape (this is due to the way ImageBrush works)
                    clone.Mutate(x => x.Crop((SixLabors.ImageSharp.Rectangle)star.Bounds));

                    // use an image brush to apply section of cloned image as the source for filling the shape
                    var brush = new ImageBrush(clone);

                    // now fill the shape with the image brush containing the portion of 
                    // cloned image with the effects applied
                    //image.Mutate(c => c.Fill(brush, star));
                    image.Mutate(c => c.Fill(SixLabors.ImageSharp.Color.Red, star));
                }

                picbox.Image = System.Drawing.Image.FromStream(LsApp.ImgToMemoryStream(image));
                //picbox.SizeMode = PictureBoxSizeMode.AutoSize;
            }
        }

        private void btnsbrqx_Click(object sender, EventArgs e)
        {
            var simplePath = new Polygon(new LinearLineSegment(
                           new Vector2(0, 0),
                           new Vector2(200, 0),
                           new Vector2(200, 200),
                           new Vector2(0, 200)
                           ));

            var hole1 = new Polygon(new LinearLineSegment(
                            new Vector2(37, 85),
                            new Vector2(93, 85),
                            new Vector2(65, 137)));
            IPath clipped = simplePath.Clip(hole1);

            var simplePath2 = new Polygon(new LinearLineSegment(
                          new Vector2(0, 300),
                          new Vector2(0, 500),
                          new Vector2(200, 500),
                          new Vector2(200, 300)
                          ));



            //IPath clipped2 = simplePath2.Clip(new EllipsePolygon(150, 450 , 50));
            
            IPath clipped2 = simplePath2.Clip(new EllipsePolygon(100, 350, 50));
            using (SixLabors.ImageSharp.Image image = new SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgba32>(600, 400))
            {
               
                float rotate = 90;
                image.Mutate(x => x.BackgroundColor(SixLabors.ImageSharp.Color.Grey).Fill(SixLabors.ImageSharp.Color.GreenYellow, clipped).Rotate(rotate));

                image.Mutate(x => x.Fill(SixLabors.ImageSharp.Color.BlueViolet, clipped2));
                picbox.Image = System.Drawing.Image.FromStream(LsApp.ImgToMemoryStream(image));
            }
        }
    }
}
